;(function ($, window, undefined) {

	var $allDropdowns = $();

	$.fn.dropdownHover = function (options) {

	    if('ontouchstart' in document) return this; 
	    $allDropdowns = $allDropdowns.add(this.parent());

	    return this.each(function () {
	        var $this = $(this),
	            $parent = $this.parent(),
	            defaults = {
	                delay: 10,
	                instantlyCloseOthers: true
	            },
	            data = {
	                delay: $(this).data('delay'),
	                instantlyCloseOthers: $(this).data('close-others')
	            },
	            showEvent   = 'show.bs.dropdown',
	            hideEvent   = 'hide.bs.dropdown',
	           
	            settings = $.extend(true, {}, defaults, options, data),
	            timeout;

	        $parent.hover(function (event) {
	            if(!$parent.hasClass('open') && !$this.is(event.target)) {
	              
	                return true;
	            }

	            openDropdown(event);
	        }, function () {
	            timeout = window.setTimeout(function () {
	                $parent.removeClass('open');
	                $this.trigger(hideEvent);
	            }, settings.delay);
	        });

	        $this.hover(function (event) {

	            if(!$parent.hasClass('open') && !$parent.is(event.target)) {
	    
	                return true;
	            }

	            openDropdown(event);
	        });


	        $parent.find('.dropdown-submenu').each(function (){
	            var $this = $(this);
	            var subTimeout;
	            $this.hover(function () {
	                window.clearTimeout(subTimeout);
	                $this.children('.dropdown-menu').show();
	                $this.siblings().children('.dropdown-menu').hide();
	            }, function () {
	                var $submenu = $this.children('.dropdown-menu');
	                subTimeout = window.setTimeout(function () {
	                    $submenu.hide();
	                }, settings.delay);
	            });
	        });

	        function openDropdown(event) {
	            $allDropdowns.find(':focus').blur();

	            if(settings.instantlyCloseOthers === true)
	                $allDropdowns.removeClass('open');

	            window.clearTimeout(timeout);
	            $parent.addClass('open');
	            $this.trigger(showEvent);
	        }
	    });
	};

	$(document).ready(function () {
	    
	    $('[data-hover="dropdown"]').dropdownHover();
	});

})(jQuery, this);

	if(user){

		var user_xp = user.LV;
		var user_ex = 0;
		var user_percent = '0%';
	


		if (user_xp <=200) {
			user_ex = 1;
			user_percent = (user_xp/200)*100+"%";
		} else if (user_xp <=1000) {
			user_ex = 2;
			user_percent = (user_xp/1000)*100+"%";
		} else if (user_xp <=5000) {
			user_ex = 3;
			user_percent = (user_xp/5000)*100+"%";
		} else if (user_xp <=15000) {
			user_ex = 4;
			user_percent = (user_xp/15000)*100+"%";
		} else if (user_xp <=50000) {
			user_ex = 5;
			user_percent = (user_xp/50000)*100+"%";
		} else if (user_xp <=200000) {
			user_ex = 6;
			user_percent = (user_xp/200000)*100+"%";
		}

	}

	Date.prototype.format =function(format)
	{
	var o = {
	"M+" : this.getMonth()+1, //month
	"d+" : this.getDate(), //day
	"h+" : this.getHours(), //hour
	"m+" : this.getMinutes(), //minute
	"s+" : this.getSeconds(), //second
	"q+" : Math.floor((this.getMonth()+3)/3), //quarter
	"S" : this.getMilliseconds() //millisecond
	}
	if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
	(this.getFullYear()+"").substr(4- RegExp.$1.length));
	for(var k in o)if(new RegExp("("+ k +")").test(format))
	format = format.replace(RegExp.$1,
	RegExp.$1.length==1? o[k] :
	("00"+ o[k]).substr((""+ o[k]).length));
	return format;
	}
	
	var socket=io.connect('/');

window.onload = function(){

	if ($("#nav-move-lvnum").length > 0) {
		$("#nav-move-lvnum").html(user_ex);
	} 

	if ($("#nav-move-lvpoint").length > 0) {
		$("#nav-move-lvpoint").css("width",user_percent);
	}
	if($('#nav-more-out').length > 0){
			$('#nav-more-out').click(function() {
				$.ajax({
				url:'/log/outlogin',
				type:'get',
				cache:false,
				success:function(data){
					window.location.reload();
				}
			})
		});
	}

	Messenger.options = {
		extraClasses: 'messenger-fixed messenger-on-bottom messenger-on-right',
		theme: 'air'
	}

	if('<%-user%>'!='' && '<%-user%>'!=null){
		socket.emit("join",{'name':user.name});

		socket.on("news", function (msg) {
			Messenger().post({
								  message:msg.msg,
								  hideAfter: 1,
							});
		});
	} 

	// $('.dropdown-toggle').dropdownHover();
$('a.dropdown-toggle').one('click',function(){ location.href= $(this).attr('href'); });
}
